package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.decisiontree.pojo.dto.DecisionTreeGetDTO;
import com.xbongbong.pro.decisiontree.pojo.dto.DecisionTreeSaveDTO;
import com.xbongbong.pro.decisiontree.pojo.vo.DecisionTreeGetUpdateTimeVO;
import com.xbongbong.pro.decisiontree.pojo.vo.DecisionTreeGetVO;
import com.xbongbong.pro.decisiontree.pojo.vo.DecisionTreeSaveVO;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.DecisionTreeService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 决策树
 *
 * @Author: hongxiao
 * @date: 2021-11-24 18:55
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/decisionTree")
public class MobileDecisionTreeController {
    @Resource
    private DecisionTreeService decisionTreeService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /***
     * 获取决策树
     *
     * @param decisionTreeGetDTO 入参
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-11-24 18:57
     * @since
     * @version
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid DecisionTreeGetDTO decisionTreeGetDTO, BindingResult br) throws XbbException {
        XbbResponse<DecisionTreeGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.CONTACT.getCode(), null, DistributorMarkEnum.OTHER.getCode());
                DecisionTreeGetVO decisionTreeGetVO = decisionTreeService.get(decisionTreeGetDTO);
                response = new XbbResponse<>(decisionTreeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /***
     * 获取更新时间
     *
     * @param decisionTreeGetDTO 入参
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2022-01-21 10:52
     * @since
     * @version
     */
    @RequestMapping(value = "/getUpdateTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getUpdateTime(@RequestBody @Valid DecisionTreeGetDTO decisionTreeGetDTO, BindingResult br) throws XbbException {
        XbbResponse<DecisionTreeGetUpdateTimeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DecisionTreeGetUpdateTimeVO decisionTreeGetUpdateTimeVO = decisionTreeService.getUpdateTime(decisionTreeGetDTO);
                response = new XbbResponse<>(decisionTreeGetUpdateTimeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /***
     * 保存决策树
     *
     * @param decisionTreeSaveDTO 入参
     * @param: br
     * @throws
     * @return java.lang.String
     * @author hongxiao
     * @date 2021-11-24 19:49
     * @since
     * @version
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String save(@RequestBody @Valid DecisionTreeSaveDTO decisionTreeSaveDTO, BindingResult br) throws XbbException {
        XbbResponse<DecisionTreeSaveVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(decisionTreeSaveDTO.getIgnoreConCheck())) {
                //重复提交校验
                String uniqueFlag = decisionTreeSaveDTO.getCorpid() + decisionTreeSaveDTO.getBusinessType() + decisionTreeSaveDTO.getDataId();
                if (!paasRedisHelper.checkConcurrentLock(uniqueFlag)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                DecisionTreeSaveVO decisionTreeSaveVO = decisionTreeService.save(decisionTreeSaveDTO);
                response = new XbbResponse<>(decisionTreeSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
